package com.wei.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

import java.util.Objects;

@Configuration
public class KeyResolverConfiguration {

    // 限流规则
    @Bean
    public KeyResolver ruleKeyResolver() {
        // URI 限流
        // return exchange -> Mono.just(exchange.getRequest().getURI().getPath());
        // 参数限流
        // return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getQueryParams().getFirst("userId")));
        // ip 限流
        return exchange -> Mono.just(Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getHostName());
    }
}
